package com.usefullc.demo.controller;

import com.usefullc.demo.domain.Demo;
import com.usefullc.demo.param.DemoParam;
import com.usefullc.demo.query.DemoQuery;
import com.usefullc.demo.service.DemoService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * @author: Sampson
 * @date: 2022-06-21 16:03
 */
@RestController
@RequestMapping("/demo/demo/")
@Api( tags="Demo管理")
public class DemoController extends BaseController {

    @Autowired
    private DemoService demoService;

    @ApiOperation(value = "演示列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(DemoQuery query) {
        Pager<Demo> page = demoService.findByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperation(value = "示例详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID", required = true, example = "1") @Valid @RequestParam Long id) {
        Demo demo = demoService.findById(id);
        return WebResultBuilder.successResult(demo);
    }


    /**
     * 添加
     */
    @ApiOperation(value = "创建示例", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    public WebResult save(@RequestBody DemoParam param) {
        Demo demo = BeanUtils.beanCopy(param, Demo.class);
        return successResult(demoService.save(demo));
    }
    /**
     * 修改
     */
    @ApiOperation(value = "创建示例", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@RequestBody DemoParam param) {
        Demo oldBean = demoService.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        return successResult(demoService.save(oldBean));
    }

    /**
     * 根据id删除示例
     */
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除示例",response = WebResult.class)
    public WebResult del(@RequestBody IdsParam idParam) {
        demoService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    /**
     * 根据id删除示例
     */
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "删除示例",response = WebResult.class)
    public WebResult delByLogic(@RequestBody IdsParam idParam) {
        demoService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }






}
